package org.csu.laboratoryequipment.service.impl;

import org.csu.laboratoryequipment.common.CommonResponse;
import org.csu.laboratoryequipment.entity.Equipment;
import org.csu.laboratoryequipment.entity.User;
import org.csu.laboratoryequipment.persistence.EquipmentMapper;
import org.csu.laboratoryequipment.persistence.UserMapper;
import org.csu.laboratoryequipment.service.EquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.Link;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("equipmentService")
public class EquipmentServiceImpl implements EquipmentService {
    @Autowired
    private EquipmentMapper equipmentMapper;

    @Override
    public CommonResponse<List<Equipment>> getEquipmentList() {
        List<Equipment> equipmentList = equipmentMapper.selectList(null);
        List<Link> linkList = new ArrayList<>();
        //自己的link
        Link link1 = new Link("http://localhost:8088/equipment/equipments", "self");
        linkList.add(link1);
        //页面中可能存在的link
        for (int i = 0; i < equipmentList.size(); i++) {
            String id = equipmentList.get(i).getEquipmentID();
            Link link = new Link("http://localhost:8088/equipment/equipments/"+id, "equipment");
            linkList.add(link);
        }
        return CommonResponse.createForSuccessMessage("SUCCESS", equipmentList,linkList);
    }

    @Override
    public CommonResponse<Equipment> getEquipmentById(String equipmentId) {
        Equipment equipment = equipmentMapper.selectById(equipmentId);
        System.out.println(equipment);
        List<Link> linkList = new ArrayList<>();
        String name = equipment.getName();
        //自己的link
        Link link1 = new Link("http://localhost:8088/equipment/equipments/"+name, "self");
        linkList.add(link1);
        return CommonResponse.createForSuccessMessage("SUCCESS", equipment,linkList);
    }

    @Override
    public CommonResponse<List<Equipment>> deleteEquipmentById(String equipmentId) {

        equipmentMapper.deleteById(equipmentId);
        List<Equipment> equipmentList = equipmentMapper.selectList(null);
        List<Link> linkList = new ArrayList<>();
        //自己的link
        Link link1 = new Link("http://localhost:8088/equipments", "self");
        linkList.add(link1);
        //页面中可能存在的link
        for (int i = 0; i < equipmentList.size(); i++) {
            String id = equipmentList.get(i).getEquipmentID();
            Link link = new Link("http://localhost:8088/equipments/"+id, "equipment");
            linkList.add(link);
        }
        return CommonResponse.createForSuccessMessage("删除成功", equipmentList,linkList);
    }

    @Override
    public CommonResponse<List<Equipment>> postEquipment(String equipmentId, String name, int quantity, String status, String specifications, Date purchaseDate, double purchasePrice, String remarks) {

        Equipment equipment = new Equipment(equipmentId,name,quantity,status,specifications,purchaseDate,purchasePrice,remarks);
        equipmentMapper.insert(equipment);
        List<Equipment> equipmentList = equipmentMapper.selectList(null);
        System.out.println(equipmentList);
        List<Link> linkList = new ArrayList<>();
        //自己的link
        Link link1 = new Link("http://localhost:8088/equipments", "self");
        linkList.add(link1);
        for (int i = 0; i < equipmentList.size(); i++) {
            String id = equipmentList.get(i).getEquipmentID();
            Link link = new Link("http://localhost:8088/equipments/"+id, "equipment");
            linkList.add(link);
        }
        return CommonResponse.createForSuccessMessage("添加成功", equipmentList,linkList);
    }

    @Override
    public CommonResponse<List<Equipment>> putEquipment(String equipmentId, String name, int quantity, String status, String specifications, Date purchaseDate, double purchasePrice, String remarks) {
        Equipment equipment = new Equipment(equipmentId,name,quantity,status,specifications,purchaseDate,purchasePrice,remarks);
        equipmentMapper.updateById(equipment);
        List<Equipment> equipmentList = equipmentMapper.selectList(null);
        List<Link> linkList = new ArrayList<>();
        //自己的link
        Link link1 = new Link("http://localhost:8088/equipments", "self");
        linkList.add(link1);
        //页面中可能存在的link
        for (int i = 0; i < equipmentList.size(); i++) {
            String id = equipmentList.get(i).getEquipmentID();
            Link link = new Link("http://localhost:8088/equipments/"+id, "equipment");
            linkList.add(link);
        }
        return CommonResponse.createForSuccessMessage("更新成功", equipmentList,linkList);
    }
}
